package com.ruoyi.gxdj.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.gxdj.Vo.IdentityVo;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gxdj.domain.InfoIdentityUp;
import com.ruoyi.gxdj.service.IInfoIdentityUpService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 身份晋升Controller
 * 
 * @author ruoyi
 * @date 2024-03-18
 */
@RestController
@RequestMapping("/InfoIdentityUp/identity")
public class InfoIdentityUpController extends BaseController
{
    @Autowired
    private IInfoIdentityUpService infoIdentityUpService;

    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 查询身份晋升列表
     */
    @GetMapping("/list")
    public TableDataInfo list(InfoIdentityUp infoIdentityUp)
    {
        startPage();
        List<IdentityVo> list = infoIdentityUpService.selectInfoIdentityUpListAll(infoIdentityUp);
        return getDataTable(list);
    }

    @GetMapping("/listUserById/{userId}")
    public TableDataInfo listlistUserById(@PathVariable("userId") Long userId)
    {
        startPage();
        List<InfoIdentityUp> list = infoIdentityUpService.listlistUserById(userId);
        return getDataTable(list);
    }

    /**
     * 导出身份晋升列表
     */
    @Log(title = "身份晋升", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InfoIdentityUp infoIdentityUp)
    {
        List<InfoIdentityUp> list = infoIdentityUpService.selectInfoIdentityUpList(infoIdentityUp);
        ExcelUtil<InfoIdentityUp> util = new ExcelUtil<InfoIdentityUp>(InfoIdentityUp.class);
        util.exportExcel(response, list, "身份晋升数据");
    }

    /**
     * 获取身份晋升详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(infoIdentityUpService.selectInfoIdentityUpById(id));
    }

    /**
     * 新增身份晋升
     */
    @Log(title = "身份晋升", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InfoIdentityUp infoIdentityUp)
    {

        SysUser user = iSysUserService.selectUserById(SecurityUtils.getUserId());
        infoIdentityUp.setCreateBy(user.getNickName());
        infoIdentityUp.setUserId(user.getUserId());
        return toAjax(infoIdentityUpService.insertInfoIdentityUp(infoIdentityUp));
    }

    /**
     * 修改身份晋升
     */
    @Log(title = "身份晋升", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InfoIdentityUp infoIdentityUp)
    {

        if (infoIdentityUp.getStatus() == 2 && StringUtils.isNotEmpty(infoIdentityUp.getCurrentIdentity())){
            SysUser user1 = iSysUserService.selectUserById(infoIdentityUp.getUserId());
            SysUser user = new SysUser();
            user.setUserId(infoIdentityUp.getUserId());
            user.setIdentity(user1.getIdentity()+1);
            iSysUserService.updateUser(user);
        }
        return toAjax(infoIdentityUpService.updateInfoIdentityUp(infoIdentityUp));
    }

    /**
     * 删除身份晋升
     */
    @Log(title = "身份晋升", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(infoIdentityUpService.deleteInfoIdentityUpByIds(ids));
    }
}
